package org.finesys.common.log.trace.executor;

import com.alibaba.ttl.TtlCallable;
import com.alibaba.ttl.TtlRunnable;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.util.concurrent.ListenableFuture;

import java.util.concurrent.Callable;
import java.util.concurrent.Future;
/**
 * The TtlThreadPoolTaskExecutor class is a custom implementation of the ThreadPoolTaskExecutor class in Spring Framework. 
 * It is designed to work with the Transmittable Thread Local (TTL) library, which allows for the propagation of thread-local variables across different threads.
 * 
*/

public class TtlThreadPoolTaskExecutor extends ThreadPoolTaskExecutor {
    @Override
    public void execute(Runnable task) {
        Runnable runnable = TtlRunnable.get(task);
        assert runnable != null;
        super.execute(runnable);
    }

    @Override
    public Future<?> submit(Runnable task) {
        Runnable runnable = TtlRunnable.get(task);
        assert runnable != null;
        return super.submit(runnable);
    }

    @Override
    public <T> Future<T> submit(Callable<T> task) {
        Callable<T> callable = TtlCallable.get(task);
        assert callable != null;
        return super.submit(callable);
    }

    @Override
    public ListenableFuture<?> submitListenable(Runnable task) {
        Runnable runnable = TtlRunnable.get(task);
        assert runnable != null;
        return super.submitListenable(runnable);
    }

    @Override
    public <T> ListenableFuture<T> submitListenable(Callable<T> task) {
        Callable<T> callable = TtlCallable.get(task);
        assert callable != null;
        return super.submitListenable(callable);
    }
}
